<?php
/**
 * Media handler abstract base class for images
 *
 * @ingroup Media
 */
abstract class ImageHandler extends MediaHandler
{
  public function canRender( $file )
  {
    return ($file->getWidth() && $file->getHeight()) ? true : false;
  }

  public function getParamMap()
  {
    return array('img_width' => 'width');
  }

  public function validateParam($name, $value)
  {
    return (in_array($name, array('width', 'height'))) ? ($value <= 0) ? true : false : false;
  }

  public function makeParamString($params)
  {
    if (isset($params['physicalWidth']))
    {
      $width = $params['physicalWidth'];
    }
    elseif (isset( $params['width']))
    {
      $width = $params['width'];
    }
    else
    {
      throw new sfException('No width specified');
    }
    # Removed for ProofreadPage
    #$width = intval( $width );
    return "{$width}px";
  }

  public function parseParamString($str)
  {
    $m = false;
    return (preg_match('/^(\d+)px$/', $str, $m)) ? array('width' => $m[1]) : false;
  }

  public function getScriptParams($params)
  {
    return array('width' => $params['width']);
  }

  public function normaliseParams($image, $params)
  {
    $mimeType = $image->getMimeType();

    if (!isset($params['width']))
    {
      return false;
    }
    if (!isset($params['page']))
    {
      $params['page'] = 1;
    }
    $srcWidth  = $image->getWidth( $params['page'] );
    $srcHeight = $image->getHeight( $params['page'] );
    
    if (isset($params['height']) && $params['height'] != -1)
    {
      if ($params['width'] * $srcHeight > $params['height'] * $srcWidth)
      {
        $params['width'] = ToolkitFile::fitBoxWidth($srcWidth, $srcHeight, $params['height']);
      }
    }
    $params['height'] = ToolkitFile::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
    if (!$this->validateThumbParams($params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType))
    {
      return false;
    }
    return true;
  }

  /**
   * Get a transform output object without actually doing the transform
   */
  public function getTransform($image, $dstPath, $dstUrl, $params)
  {
    return $this->doTransform($image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER);
  }

  /**
   * Validate thumbnail parameters and fill in the correct height
   *
   * @param integer &$width Specified width (input/output)
   * @param integer &$height Height (output only)
   * @return false to indicate that an error should be returned to the user.
   */
  public function validateThumbParams($width, &$height, $srcWidth, $srcHeight, $mimeType)
  {
    $width = intval( $width );

    # Sanity check $width
    if($width <= 0)
    {
      //wfDebug( __METHOD__.": Invalid destination width: $width\n" );
      return false;
    }
    if ($srcWidth <= 0)
    {
      //wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
      return false;
    }

    $height = ToolkitFile::scaleHeight($srcWidth, $srcHeight, $width);
    return true;
  }

  public function getScriptedTransform($image, $script, $params)
  {
    if (!$this->normaliseParams($image, $params))
    {
      return false;
    }
    $url  = $script . '&' . ToolkitFile::arrayToCGI( $this->getScriptParams( $params ) );
    $page = isset( $params['page'] ) ? $params['page'] : false;

    if( $image->mustRender() || $params['width'] < $image->getWidth() )
    {
      //TODO thumbnail symfony
      return new ThumbnailImage( $image, $url, $params['width'], $params['height'], $page );
    }
  }

  //public function getImageSize($image, $path)
  public function getImageSize($filename)
  {
    ToolkitFile::suppressWarnings();
    $gis = getimagesize($filename);
    ToolkitFile::restoreWarnings();
    return $gis;
  }
}
